package com.ruoyi.web.controller.business;

import java.util.List;

import com.ruoyi.business.domain.BusContract;
import com.ruoyi.business.service.IBusContractService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusContractInvoice;
import com.ruoyi.business.service.IBusContractInvoiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同开票Controller
 * 
 * @author ruoyi
 * @date 2021-09-02
 */
@RestController
@RequestMapping("/business/invoice")
public class BusContractInvoiceController extends BaseController
{
    @Autowired
    private IBusContractInvoiceService busContractInvoiceService;

    @Autowired
    private IBusContractService busContractService;

    /**
     * 查询合同开票列表
     */
   // @PreAuthorize("@ss.hasPermi('business:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusContractInvoice busContractInvoice)
    {
        startPage();
        List<BusContractInvoice> list = busContractInvoiceService.selectBusContractInvoiceList(busContractInvoice);
        return getDataTable(list);
    }

    /**
     * 导出合同开票列表
     */
   // @PreAuthorize("@ss.hasPermi('business:invoice:export')")
    @Log(title = "合同开票", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusContractInvoice busContractInvoice)
    {
        BusContract busContract = busContractService.selectBusContractByContractId(busContractInvoice.getContractId());
        List<BusContractInvoice> list = busContractInvoiceService.selectBusContractInvoiceList(busContractInvoice);
        ExcelUtil<BusContractInvoice> util = new ExcelUtil<BusContractInvoice>(BusContractInvoice.class);
        //return util.exportExcel(list, "合同开票数据");
        return util.exportExcel(list, "合同编号【"+busContract.getContractNo()+"】开票信息");
    }

    /**
     * 获取合同开票详细信息
     */
   // @PreAuthorize("@ss.hasPermi('business:invoice:query')")
    @GetMapping(value = "/{invoiceId}")
    public AjaxResult getInfo(@PathVariable("invoiceId") Long invoiceId)
    {
        return AjaxResult.success(busContractInvoiceService.selectBusContractInvoiceByInvoiceId(invoiceId));
    }

    /**
     * 新增合同开票
     */
   // @PreAuthorize("@ss.hasPermi('business:invoice:add')")
    @Log(title = "合同开票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusContractInvoice busContractInvoice)
    {
        busContractInvoice.setCreateBy(getUsername());
        return toAjax(busContractInvoiceService.insertBusContractInvoice(busContractInvoice));
    }

    /**
     * 修改合同开票
     */
   // @PreAuthorize("@ss.hasPermi('business:invoice:edit')")
    @Log(title = "合同开票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusContractInvoice busContractInvoice)
    {
        busContractInvoice.setUpdateBy(getUsername());
        return toAjax(busContractInvoiceService.updateBusContractInvoice(busContractInvoice));
    }

    /**
     * 删除合同开票
     */
   // @PreAuthorize("@ss.hasPermi('business:invoice:remove')")
    @Log(title = "合同开票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{invoiceIds}")
    public AjaxResult remove(@PathVariable Long[] invoiceIds)
    {
        return toAjax(busContractInvoiceService.deleteBusContractInvoiceByInvoiceIds(invoiceIds));
    }
}
